const App = getApp();
const utils = require("../../utils/util.js");
import * as echarts from '../../ec-canvas/echarts';
let chartLine
let chartLine1
function getOption(remind,report,summary) {
  var option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      top: '5%',
      left: 'center',
      textStyle: { //图例文字的样式
        // color: '#fff',
        fontSize: 14
      },
    },
    series: [
      {
        name: '访问来源',
        type: 'pie',
        radius: '35%',
        radius: ['40%', '60%'],
        center: ['50%', '60%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2,
          normal: {
            label: {
              show: true,
              formatter: ' {b} : \n {c}({d}%)'
            },
            labelLine: { show: true }
          },
          fontSize: '30',
        },
        label: {
          show: false,
          position: 'center',
          normal: {
            textStyle: {
              fontSize: 11
            }
          }
        },
        emphasis: {
          label: {
            show: true,
            // fontSize: '30',
            // fontWeight: 'bold'
          }
        },
        labelLine: {
          normal: {
            show: true,
            length: 7
          }
        },
        data: [
          { value: remind, name: '预约带看' },
          { value: report, name: '预约报备', itemStyle: { color: "#ff5e22" } },
          { value: summary, name: '实际带看' },
        ]
      }
    ]
  };
  return option;
}
function getOption1(yData, x_arr, x_data) {
  var option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {            // Use axis to trigger tooltip
        type: 'shadow'        // 'shadow' as default; can also be 'line' or 'shadow'
      }
    },

    legend: {
      data: x_arr,
      itemHeight: 12,
      itemWidth: 12,
      selectedMode: true,
      textStyle: { //图例文字的样式
        fontSize: 13
      },
    },
    grid: {
      left: '2%',
      right: '6%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'value',
      max: function (value) {//取最大值向上取整为最大刻度
        return Math.ceil(value.max)
      },
    },
    // ['组一', '组二', '组三', '组四']
    yAxis: {
      type: 'category',
      data: yData,
      axisLabel: {
        interval: 0,
        // rotate: 60,
        formatter: function (value) {
          var str = "";
          var num = 3; //每行显示字数 
          var valLength = value.length; //该项x轴字数  
          var rowNum = Math.ceil(valLength / num); // 行数  

          if (rowNum > 1) {
            for (var i = 0; i < rowNum; i++) {
              var temp = "";
              var start = i * num;
              var end = start + num;

              temp = value.substring(start, end) + "\n";
              str += temp;
            }
            return str;
          } else {
            return value;
          }
        },
        textStyle: {
          show: true,
          fontFamily: '微软雅黑',
          fontSize: '12',
        },
      }
    },
    // [10,11,13,14]
    series: [
      {
        name: x_arr[0],
        type: 'bar',
        stack: 'total',
        label: {
          formatter: function (params) {
            if (params.value > 0) {
              return params.value;
            } else {
              return ' ';
            }
          },
          show: true,
          textStyle: {
            fontSize: 13,
            color: '#fff'
          }
        },
        emphasis: {
          focus: 'series'
        },
        data: x_data.inspect_remind,
        barCategoryGap: '41%',
        color: '#5470c6',
        barBorderRadius: [20, 20, 0, 0],
        borderWidth: 0,
        borderColor: '#333',
      },
      {
        name: x_arr[1],
        type: 'bar',
        stack: 'total',
        label: {
          formatter: function (params) {
            if (params.value > 0) {
              return params.value;
            } else {
              return ' ';
            }
          },
          show: true,
          textStyle: {
            fontSize: 13,
            color: '#fff'
          }
        },
        emphasis: {
          focus: 'series'
        },
        data: x_data.inspect_report,
        color: '#91cc75',
        barBorderRadius: [20, 20, 0, 0],
        borderWidth: 0,
        borderColor: '#333',
      },
      {
        name: x_arr[2],
        type: 'bar',
        stack: 'total',
        label: {
          formatter: function (params) {
            if (params.value > 0) {
              return params.value;
            } else {
              return ' ';
            }
          },
          show: true,
          textStyle: {
            fontSize: 13,
            color: '#fff'
          }
        },
        emphasis: {
          focus: 'series'
        },
        data: x_data.inspect_summary,
        color: '#fac858',
        barBorderRadius: [20, 20, 0, 0],
        borderWidth: 0,
        borderColor: '#333',
      },
      {
        name: x_arr[3],
        type: 'bar',
        stack: 'total',
        label: {
          formatter: function (params) {
            if (params.value > 0) {
              return params.value;
            } else {
              return ' ';
            }
          },
          show: true,
          textStyle: {
            fontSize: 13,
            color: '#fff'
          }
        },
        emphasis: {
          focus: 'series'
        },
        data: x_data.turnover_pass,
        color: '#ee6666',
        barBorderRadius: [20, 20, 0, 0],
        borderWidth: 0,
        borderColor: '#333',
      }
    ]
  };
  return option;
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    show_pop:false,
    rate_num : "",
    rate:'',
    role_id:'',
    comp:'较昨日',
    currentIndex: 2,
    currentIndexY: 0,
    ttotalData: {},
    all_turnover:{},
    clue_data: {},
    customer_data: {},
    inspect_data: {},
    line_data: {},
    resource_data: {},
    tabs: [
      { index: 0, name: '日报', type: 'day' },
      { index: 1, name: '周报', type: 'week' },
      { index: 2, name: '月报', type: 'month' },
    ],
    ecLine: {
      onInit: function (canvas, width, height,dpr) {
        //初始化echarts元素，绑定到全局变量，方便更改数据
        chartLine = echarts.init(canvas, null, {
          width: width,
          height: height,
          devicePixelRatio: dpr
        });
        canvas.setChart(chartLine);

        //可以先不setOption，等数据加载好后赋值，
        //不过那样没setOption前，echats元素是一片空白，体验不好，所有我先set。
        // var option = getOption();
        // chartLine.setOption(option);
        return chartLine
      }
    },
    ecLine1: {
      onInit: function (canvas, width, height,dpr) {
        //初始化echarts元素，绑定到全局变量，方便更改数据
        chartLine1 = echarts.init(canvas, null, {
          width: width,
          height: height,
          devicePixelRatio: dpr
        });
        canvas.setChart(chartLine1);

        //可以先不setOption，等数据加载好后赋值，
        //不过那样没setOption前，echats元素是一片空白，体验不好，所有我先set。
        // var option = getOption();
        // chartLine.setOption(option);
        return chartLine1
      }
    },
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  handleChange(e){
    var token = wx.getStorageSync('token');
    App._post_form('login/login_out', {
      token: token
    }, function (result) {
      console.log(result);
      // 删除记录
      wx.removeStorageSync('token');
      wx.removeStorageSync('mem_id');
      wx.removeStorageSync('role_id');
      wx.reLaunch({
        url: '../login/login',
      })
    }, false, function () {
      wx.hideLoading();
    });

    //  wx.reLaunch({
    //    url: '../login/login',
    //  })
  },
    /**
   * tab切换
   */
  handleTabChange(e) {
    let comp
    if(e.currentTarget.dataset.index == 0){
      comp = '较昨日'
    }else if(e.currentTarget.dataset.index == 1){
      comp = '较上周'
    }else{
      comp = '较上月'
    }
    this.setData({
      currentIndex: e.currentTarget.dataset.index,
      comp
    })

    this.getTeamData1()
  },

  handleShowPop1(){
    wx.navigateTo({
      url: '../user-detail/index',
    })
  },
  handleShowPop2(){
    wx.navigateTo({
      url: '../user-detail2/index',
    })
  },
  handleShowPop(){
    this.setData({
      show_pop:true
    })
  },
  handleHidePop(){
    this.setData({
      show_pop:false
    })
  },

  //个人数据
  getTeamData() {
    let _this = this
    App._post_form('DataStats/dataCenter', {
      time_type: _this.data.tabs[_this.data.currentIndex].type,
      type: 'personal'
    }, function (result) {
      _this.setData({
        totalData: result.data,
        all_turnover:result.data.all_turnover,
        clue_data: result.data.clue_data,
        customer_data: result.data.customer_data,
        inspect_data: result.data.inspect_data,
        resource_data: result.data.resource_data
      })
      let inspect_remind = result.data.inspect_data.inspect_remind.this_date
      let inspect_report = result.data.inspect_data.inspect_report.this_date
      let inspect_summary = result.data.inspect_data.inspect_summary.this_date
      var option = getOption(inspect_remind, inspect_report, inspect_summary);
      // // console.log(chartLine)
      chartLine.setOption(option);
      let rate_num = Number(result.data.turnover_rate)
      let num = Math.round(rate_num.toFixed(2) * 100 /2.5)
      _this.setData({
        rate_num:num,
        rate:Number(rate_num.toFixed(2) * 100)
      })
      console.log(num)
    })
  },
  //获取团队数据
  getTeamData1() {
    let _this = this
    console.log( _this.data.currentIndex);
    App._post_form('DataStats/dataCenter', {
      time_type: _this.data.tabs[_this.data.currentIndex].type,
      type: 'team'
    }, function (result) {
      _this.setData({
        totalData: result.data,
        all_turnover:result.data.all_turnover,
        clue_data: result.data.clue_data,
        customer_data: result.data.customer_data,
        inspect_data: result.data.inspect_data,
        line_data: result.data.line_data,
        resource_data: result.data.resource_data
      })
      let { y_title, x_arr, x_data } = result.data.line_data
      var option = getOption1(y_title, x_arr, x_data);
      // console.log(chartLine)
      chartLine1.setOption(option);
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      role_id:wx.getStorageSync('role_id')
    })
    setTimeout(() => {
      this.getTeamData1()
    }, 500);
    console.log(this.data.role_id)
  },

  /**
   * 获取客户列表数据
   */


  onReady: function () {
    //页面渲染完成 第三运行
    //console.log('onReady');
  },
/**
   * 下拉刷新
   */
  onPullDownRefresh: function () {
    // 获取首页数据
    this.onShow()
  },

})